package com.yycx.module.bus.provider.handler.process.task;

import com.yycx.bpm.provider.listener.FlowPageInterceptor;
import com.yycx.bpm.provider.model.TaskResponse;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.module.admin.client.entity.BaseUser;
import com.yycx.module.admin.provider.service.BaseUserService;
import com.yycx.module.bus.client.entity.BusConsumption;
import com.yycx.module.bus.client.entity.BusPaymenttype;
import com.yycx.module.bus.provider.service.*;
import org.flowable.common.engine.api.query.Query;
import org.flowable.task.api.TaskInfoQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 日常消费审批任务列表
 *
 * @author zyf
 */
@Component
public class ConsumptionTaskTodoListHandler implements FlowPageInterceptor<TaskResponse> {

    @Autowired
    private BusConsumptionService busConsumptionService;
    @Autowired
    private BaseUserService baseUserService;

    @Autowired
    private BusPaymenttypeService busPaymenttypeService;

    Map checkMap = new HashMap();
    @Override
    public void prepare(Query query, EntityMap params) {
        TaskInfoQuery taskQuery = (TaskInfoQuery) query;
        String companyId = OpenHelper.getCompanyId().toString();
        String consumptionNo = params.get("consumptionNo");
        BusConsumption consumption = busConsumptionService.getByProperty("consumptionNo", consumptionNo);
        if (FlymeUtils.isNotEmpty(consumption)) {
            taskQuery.processInstanceBusinessKey(consumption.getConsumptionId().toString());
        } else {
            taskQuery.processInstanceBusinessKey(consumptionNo);
        }
        taskQuery.taskTenantId(companyId).orderByTaskCreateTime().desc();
        taskQuery.processDefinitionKey("consumption_" + companyId);
    }

    @Override
    public List<EntityMap> complete(Query query, List<TaskResponse> data, EntityMap params) {
        List<EntityMap> list = new ArrayList<>();
        if (FlymeUtils.isNotEmpty(data)) {
            for (TaskResponse taskResponse : data) {
                String consumptionId = taskResponse.getBusinessKey();
                if (FlymeUtils.isNotEmpty(consumptionId)) {
                    EntityMap busConsumption = busConsumptionService.findByBusConsumptionId(consumptionId);
                    Long paymentTypeId=busConsumption.getLong("paymentTypeId");
                    Long applyUserId = busConsumption.getLong("applyUserId");
                    String processInstanceId = taskResponse.getProcessInstanceId();
                    String assignee = taskResponse.getAssignee();
                    BaseUser applyUser = baseUserService.getUserById(applyUserId);
                    if (FlymeUtils.isNotEmpty(applyUser)) {
                        String applyUserName = applyUser.getUserName();
                        busConsumption.put("applyUserName", applyUserName);
                    }
                    Long payUserId = busConsumption.getLong("payUserId");
                    BaseUser payUser = baseUserService.getUserById(payUserId);
                    if (FlymeUtils.isNotEmpty(payUser)) {
                        String payUserName = payUser.getNickName();
                        busConsumption.put("payUserName", payUserName);
                    }
                    busConsumption.put("assignee", taskResponse.getAssignee());
                    busConsumption.put("endTime", taskResponse.getEndTime());
                    busConsumption.put("taskId", taskResponse.getId());
                    BusPaymenttype paymentType=busPaymenttypeService.getById(paymentTypeId);
                    if(FlymeUtils.isNotEmpty(paymentType)){
                        busConsumption.put("paymentType", paymentType.getPaymentTypeCode());
                    }
                    String key = "Consumption:" + assignee + processInstanceId;

                    list.add(busConsumption);

                   // checkMap.put(key, "1");
                }
            }
        }
        return list;
    }


}
